package com.ncu.controller;

import com.github.pagehelper.PageInfo;
import com.ncu.pojo.Emp;
import com.ncu.pojo.Msg;
import com.ncu.service.EmpService;
import com.ncu.service.impl.EmpServiceImpl;
import javafx.scene.chart.ValueAxis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class EmpController {

    @Autowired
    private EmpService empService;

/*    @RequestMapping("/findAll")
    public ModelAndView findAllEmp(@RequestParam(value = "currentPage",defaultValue = "1")int currentPage,
                                               ModelAndView modelAndView ){

        PageInfo page = empService.findAllDept(currentPage);


        modelAndView.addObject("page",page);
        modelAndView.setViewName("list");
        return modelAndView;
    }*/


    @RequestMapping("/findAll")
    @ResponseBody
    public Msg FindAllEmp(@RequestParam(value = "currentPage",defaultValue = "1")int currentPage,
                          HttpServletResponse response){

        PageInfo<Emp> page = empService.findAllDept(currentPage);
        Map<String,Object> map= new HashMap<>();
        map.put("page",page);
        Msg msg=new Msg();
        msg.setStatus(response.getStatus());
        msg.setDate(1);
        msg.setMap(map);
        return msg;
    }

    @RequestMapping("/saveEmp")
    @ResponseBody
    public Msg SaveEmp(@RequestParam(value = "empName")String empName,
                       @RequestParam(value = "email")String email,
                       @RequestParam(value = "gender")String gender,
                       @RequestParam(value = "dId")int  dId,
                       HttpServletResponse response){
        Emp emp=new Emp(empName,gender,email,dId);
        int data = empService.saveEmp(emp);
        Msg msg=new Msg();
        msg.setStatus(response.getStatus());
        msg.setDate(data);
        return msg;
    }


    @RequestMapping("/checkEmpName")
    @ResponseBody
    public Msg CheckEmpName(@RequestParam(value = "empNames")String empName,
                       HttpServletResponse response){
        int data = empService.checkEmpName(empName);
        Msg msg=new Msg();
        msg.setStatus(response.getStatus());
        msg.setDate(data);
        return msg;
    }

    @RequestMapping(value = "/findEmpById/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Msg FindEmpById(@PathVariable(value = "id") int empId,
                           HttpServletResponse response){
        Emp emp = empService.findEmpById(empId);
        Map<String,Object> map= new HashMap<>();
        map.put("emp",emp);
        Msg msg=new Msg();
        msg.setStatus(response.getStatus());
        msg.setDate(1);
        msg.setMap(map);
        return msg;
    }

    @RequestMapping(value = "/updateEmp/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public Msg UpdateEmp(@PathVariable(value = "id") int empId,
                         @RequestParam(value = "email")String email,
                         @RequestParam(value = "gender")String gender,
                         @RequestParam(value = "dId")int  dId,
                         HttpServletResponse response){
        Emp emp=new Emp(empId,gender,email,dId);
        int data = empService.updateEmp(emp);
        Map<String,Object> map= new HashMap<>();
        map.put("emp",emp);
        Msg msg=new Msg();
        msg.setStatus(response.getStatus());
        msg.setDate(data);
        msg.setMap(map);
        return msg;
    }


    @RequestMapping(value = "/deleteEmp/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg DeleteEmp(@PathVariable(value = "id") int empId,
                         HttpServletResponse response){
        int data = empService.deleteEmp(empId);
        Msg msg=new Msg();
        msg.setStatus(response.getStatus());
        msg.setDate(data);
        return msg;
    }


    @RequestMapping(value = "/deleteBatch/{empIds}",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg DeleteBatch(@PathVariable(value = "empIds") String empIds,
                         HttpServletResponse response){
        List<Integer> listEmp=new ArrayList<>();
        int date=0;
        if(empIds!=""&&empIds.length()>0){
            if(empIds.contains("-")){
                String[] empIdsStr = empIds.split("-");
                for (String empId : empIdsStr) {
                    listEmp.add(Integer.parseInt(empId));
                }
                date = empService.deleteBatch(listEmp);
            }else {
                int empId=Integer.parseInt(empIds);
                date = empService.deleteEmp(empId);
            }
        }else {
            date=-1;
        }
        Msg msg=new Msg();
        msg.setStatus(response.getStatus());
        msg.setDate(date);
        return msg;
    }


}
